package openbang.account.web.portal.client;

import openbang.account.service.intf.AccountService;
import openbang.account.service.vo.AccountServiceEnum;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Document;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;


/**
 * 重置密码控制 
 * @author aibo zeng
 *
 */
public class SetupPwd implements EntryPoint {
	//private TextBox email = null;
	private PasswordTextBox pwd1 = null;
	private PasswordTextBox pwd2 = null;
	private Button  confirmBtn = null;
	private Hidden applyId = null;
	private HTML emailTitleLabel = new HTML();
	private HTML pwd1Label = new HTML();
	private HTML pwd2Label = new HTML();

	
	private void bindUI(){
		confirmBtn = Button.wrap(Document.get().getElementById("confirmBtn"));
		//email = TextBox.wrap(Document.get().getElementById("emailTxt"));
		applyId = Hidden.wrap(Document.get().getElementById("applyIdTxt"));
		pwd1   = PasswordTextBox.wrap(Document.get().getElementById("pwd1Txt"));
		pwd2   = PasswordTextBox.wrap(Document.get().getElementById("pwd2Txt"));
		RootPanel.get("emailTitle").add(emailTitleLabel);
		RootPanel.get("pwd1Title").add(pwd1Label);
		RootPanel.get("pwd2Title").add(pwd2Label);
	}	
	@Override
	public void onModuleLoad() {
		bindUI();

		pwd2.addBlurHandler(new BlurHandler() {
			public void onBlur(BlurEvent event) {
				//焦点离开，检查两次密码是否一致
				doCheckPwd();
			}
		});
		confirmBtn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				doSetupPwd();
			}
		});
		//加载完，注册按钮不管用，录入信息过程中才管用
		confirmBtn.setEnabled(false);
	}
	
	public void doSetupPwd(){
		doCheckPwd();
		if(!confirmBtn.isEnabled()){
			return;
		}
		if(applyId.getValue()==null||applyId.getValue().length()<=0){
			Window.alert("不能重复提交操作.");
			return;
		}
		AccountService.Util.getInstance().setupNewPwd(Long.parseLong(applyId.getValue()),pwd1.getText().trim(), new AsyncCallback<Integer>(){
			public void onSuccess(Integer result) {
				if(result==AccountServiceEnum.OK.getCode()){
					Window.alert("重置密码成功");
					//没有设置自动登录  toSwitchLoginAfterUrl(null);
					applyId.setValue("");
					confirmBtn.setEnabled(false);
				}
				else{
					emailTitleLabel.setHTML("<font color=\"blue\">重置密码失败:"+AccountServiceEnum.getDesc(result)+"</font>");
				}
			}
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}				
		});
	}
	
	public void doCheckPwd(){
		if(!pwd1.getText().equals(pwd2.getText())){
			pwd2Label.setHTML("<font color=\"red\">两次密码不一致</font>");
			confirmBtn.setEnabled(false);
		}
		else{
			pwd2Label.setHTML("<font color=\"blue\">两次密码一致</font>");
			confirmBtn.setEnabled(true);
		}
	}
	
	//获取登录后应该跳转的页面  在 JSP的 javascript中定义      google登录需要
	public static native String getLoginAfterUrl() /*-{  
         return parent.getLoginAfterDefaultUrl();
    }-*/;	
	//调用登录后应该跳转的页面  参数url可以为空
	public static native void toSwitchLoginAfterUrl(String url) /*-{  
         parent.toLoginAfterUrl(url);
    }-*/;		
}
